package com.sltx.controller;

import com.sltx.controller.base.BaseController;
import com.sltx.entity.SiteDto;
import com.sltx.mapper.GainMapper;
import com.sltx.service.GainService;
import com.sltx.service.SiteService;
import com.sltx.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author xue
 * @Date 2024/4/1 16:54
 */
@RestController
@RequestMapping("/gain")
@ResponseBody
@EnableScheduling
public class GainController extends BaseController {

    @Autowired
    private GainService gainService;

    //获取数据存储到数据库-地方标准信息服务平台-不需要
    @RequestMapping("/huoqu")
    public Object Huoqu() throws Exception {
        PageData pd = this.getPageData();
        return gainService.getHuoqu(pd);
    }

    //获取数据存储到数据库-行业标准信息服务平台 2024-8-20 获取近半年的
    @RequestMapping("/hangye")
    public Object Hangye() throws Exception {
        PageData pd = this.getPageData();
        return gainService.Hangye(pd);
    }

    //获取数据存储到数据库-国家标准目录查询-不需要
    @RequestMapping("/state")
    public Object State() throws Exception {
        PageData pd = this.getPageData();
        return gainService.State(pd);
    }

    //查询地方标准信息平台pk
    @RequestMapping("/dfpk")
    public Object dfkey() {
        PageData pd = this.getPageData();
        return gainService.dfkey(pd);
    }

    //查询行业标准信息平台pk
    @RequestMapping("/hypk")
    public Object hypk() {
        PageData pd = this.getPageData();
        return gainService.hypk(pd);
    }

    //查询国家标准目录id
    @RequestMapping("/stateid")
    public Object gjid() {
        PageData pd = this.getPageData();
        return gainService.stateid(pd);
    }

    //读取文件获取文件名
    @RequestMapping("/excelread")
    public Object excelread(@RequestParam MultipartFile file) {
        PageData pd = this.getPageData();
        return gainService.excelread(pd, file);
    }

    //替换entname为人名数据为‘’
    @RequestMapping("/updatename")
    public Object delete() {
        PageData pd = this.getPageData();
        return gainService.updateEntname(pd);

    }

    //替换行业中issn字段含有中文为''
    @RequestMapping("/updatezwbm")
    public Object updatezwbm() {
        PageData pd = this.getPageData();
        return gainService.updatezwbm(pd);

    }

    //替换行业中china_number字段中含有中文的为''
    @RequestMapping("/updatezgxh")
    public Object updatezgxh() {
        PageData pd = this.getPageData();
        return gainService.updatezgxh(pd);

    }

    //删除国家表中entname字段里面的无用数据
    @RequestMapping("/updatestate")
    public Object deleteentname() {
        PageData pd = this.getPageData();
        return gainService.updateState(pd);
    }

    //拆分entname字段并存入数据库多条
    @RequestMapping("/updateentname")
    public Object updateentname() {
        PageData pd = this.getPageData();
        return gainService.updateentname(pd);
    }

    //拆分entname并存入数据库多条-地方标准信息服务平台
    @RequestMapping("/updateentnameplace")
    public Object updateentnameplace() {
        PageData pd = this.getPageData();
        return gainService.updateentnameplace(pd);
    }

    //拆分entname并存入数据库多条-行业标准信息服务平台
    @RequestMapping("/updateentnameindustry")
    public Object updateentnameindustry() {
        PageData pd = this.getPageData();
        return gainService.updateentnameindustry(pd);
    }

    //拆分entname并存入数据库多条-全国标准信息公共服务平台-每周一晚上十一点
    @RequestMapping("/updateentnamestate")
    public Object updateentnamestate() {
        PageData pd = this.getPageData();
        return gainService.updateentnamestate(pd);
    }

    //将标准信息存入数据库
    @RequestMapping("/instStandardsInfo")
    public Object instStandardsInfo() {
        PageData pd = this.getPageData();
        return gainService.instStandardsInfo(pd);
    }

    //拆分团体数据的企业字段
    @RequestMapping("/updatetuanti")
    public Object updatetuanti() {
        return gainService.updatetuanti();
    }

    @Autowired
    private GainMapper gainMapper;



    //拆分行业标准信息服务平台周一晚上十一点半点
    //@Scheduled(cron = "0 0 23 ? * MON")
    public void hybiaozhuntask() {
        Map<String, Object> pd = new HashMap<>();
        gainMapper.update3();
        //查询所有数据-行业标准信息服务平台-定时任务
        List<Map<String, Object>> list = gainMapper.getEntnameindustryds(pd);
        for (int i = 0; i < list.size(); i++) {
            String[] entnames = list.get(i).get("entname").toString().split("[，、。：,＆;\\.\\s&\\\\]+");
            for (int i1 = 0; i1 < entnames.length; i1++) {
                if (entnames[i1].endsWith("等")) {
                    String entname = entnames[i1].substring(0, entnames[i1].length() - 1);
                    pd.put("entname", entname);
                } else {
                    pd.put("entname", entnames[i1]);
                }
                pd.put("code", list.get(i).get("code"));
                pd.put("industry", list.get(i).get("industry"));
                pd.put("chname", list.get(i).get("chname"));
                pd.put("pk", list.get(i).get("pk"));
                pd.put("status", list.get(i).get("status"));
                pd.put("recordno", list.get(i).get("recordno"));
                pd.put("built_date", list.get(i).get("built_date"));
                pd.put("china_number", list.get(i).get("china_number"));
                pd.put("issn", list.get(i).get("issn"));
                pd.put("recorddate", list.get(i).get("recorddate"));
                pd.put("issuedate", list.get(i).get("issuedate"));
                pd.put("actdate", list.get(i).get("actdate"));
                //将地方拆分完的数据存到数据库表-shuju_industry_cf-定时任务
                gainMapper.insteIndustryEntnameds(pd);
            }

        }
    }

    //全国标准信息公共服务平台-每周一晚上十一点
    //@Scheduled(cron = "0 30 23 ? * MON")
    public void gjbiaozhuntask() {
        Map<String, Object> pd = new HashMap<>();
        gainMapper.update4();
        //查询所有数据-全国标准信息公共服务平台-定时任务
        List<Map<String, Object>> list = gainMapper.getEntnamestateds(pd);
        for (int i = 0; i < list.size(); i++) {
            String[] entnames = list.get(i).get("entname").toString().split("[、,＆;\\.\\s&\\\\]+");
            for (int i1 = 0; i1 < entnames.length; i1++) {
                pd.put("entname", entnames[i1]);
                pd.put("C_STD_CODE", list.get(i).get("C_STD_CODE"));
                pd.put("C_C_NAME", list.get(i).get("C_C_NAME"));
                pd.put("ACT_DATE", list.get(i).get("ACT_DATE"));
                pd.put("STATE", list.get(i).get("STATE"));
                pd.put("ISSUE_DATE", list.get(i).get("ISSUE_DATE"));
                pd.put("built_date", list.get(i).get("built_date"));
                pd.put("china_number", list.get(i).get("china_number"));
                pd.put("issn", list.get(i).get("issn"));
                //将地方拆分完的数据存到数据库表-shuju_state_cf-定时任务
                gainMapper.insteStateEntnameds(pd);
            }
        }

    }


    //拆分团体
    //@Scheduled(cron = "0 0 22 * * ?")
    public void tuantitask() {
        //查询所有数据-团体数据
        Map<String, Object> pd = new HashMap<>();
        gainMapper.update1();
        List<Map<String, Object>> list = gainMapper.getEntnametuanti();
        for (int i = 0; i < list.size(); i++) {
            String[] entnames = list.get(i).get("draftunit").toString().split("[、。，；]");
            for (int i1 = 0; i1 < entnames.length; i1++) {
                pd.put("draftunit", entnames[i1]);
                pd.put("code", list.get(i).get("code"));
                pd.put("chname", list.get(i).get("chname"));
                pd.put("status", list.get(i).get("status"));
                pd.put("issuedate", list.get(i).get("issuedate"));
                pd.put("actdate", list.get(i).get("actdate"));
                int i2 = list.get(i).get("ics").toString().lastIndexOf(" ");
                if (i2 != -1) {
                    pd.put("ics", list.get(i).get("ics").toString().substring(0, i2));
                } else {
                    pd.put("ics", list.get(i).get("ics").toString());
                }
                pd.put("ccs", list.get(i).get("ccs"));
                //将地方拆分完的数据存到数据库表-shuju_tuanti_cf
                gainMapper.instEntnametuanti(pd);
            }
        }

    }

    @Autowired
    private SiteService siteService;

    //查询节点数据
    @RequestMapping("/getAllData")
    public List<SiteDto> getAllData(HttpServletRequest request) {
        PageData pd = this.getPageData();
        return siteService.queryAll(pd,request);
    }

}
